<template>
    <div class="box">
        <div class="top">
            <van-nav-bar left-arrow @click-left="$router.go(-1)" />
        </div>
        <div class="content">
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <van-field v-model="username" name="username" label="用户名" placeholder="用户名"
                        :rules="[{ required: true, message: '请填写用户名' }, { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }]" />
                    <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                        :rules="[{ required: true, message: '请填写密码' }]" />
                    <van-field v-model="sms" center clearable label="短信验证码" placeholder="请输入短信验证码"
                    :rules="[{ required: true, message: '请填写短信验证码' },{pattern: /^\d{6}$/, message: '请输入6位数字'}] " >
                        <template #button>
                            <van-button size="small" type="primary"
                             @click="sendcode" :disabled="msg!='获取验证码' || username.length !=11">
                             {{ msg }}</van-button>
                        </template>
                    </van-field>
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit" 
                   :disabled="username.length==0 || sms.length==0  || sms.length!=6" >
                        登录
                    </van-button>
                    <p @click="$router.push('/sigin')">快速注册</p>
                </div>
            </van-form>
        </div>
    </div>
</template>
<script setup>
import { ref } from 'vue';
import request from '@/utils/request'
import { showSuccessToast, showFailToast } from 'vant';

import { useRouter } from 'vue-router'
import md5 from 'blueimp-md5'
const router = useRouter()
const username = ref('');
const password = ref('');
const msg=ref('获取验证码')
const sms=ref('')
const timer=ref(null);
const count=ref(10)
const onSubmit = (values) => {
    console.log('submit', values);
    request.post('/login', {
        username:values.username,
        password:md5(values.password)
     
    }).then(res=>{
        if (res.data.code == 200) {
            // 说明登录成功
            // 提示登录成功
            showSuccessToast(res.data.message);

            // 存储token
            localStorage.setItem('token', res.data.token)
            // 存储用户信息
            localStorage.setItem('username', username.value)
            // 跳转页面
            router.push('/')
        } else {
            // 说明登录失败
            showFailToast(res.data.message);
        }
    })
};

 const sendcode=()=>{
     msg.value=count.value+'秒后重新获取'
     request.get('/code').then(res=>{
        if(res.data.code==200){
            sms.value=res.data.data
        }
     })
timer.value=setInterval(()=>{
    count.value--
    if(count.value==0){
        clearInterval(timer.value)
        msg.value='获取验证码'
        count.value=10
    }else{
        msg.value=count.value+'秒后重新获取'
    }
},1000)
 }

</script>